import time


def cost_time(func):
    """
    使用装饰器实现计算函数所耗费的时间
    """
    print("装饰器正在装饰。。。。") # 这段代码只会在装饰的时候执行一次，后续不会再执行
    def inner(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(end_time - start_time)
        return result

    return inner

# def outer(is_need_ret_val):
#     """
#     :param is_need_ret_val: 是否需要返回值 true表示需要 false表示不需要
#     :return:
#     """
#
#     def cost_time(func):
#         """
#         使用装饰器实现计算函数所耗费的时间
#         """
#
#         def inner(*args, **kwargs):
#             start_time = time.time()
#             result = func(*args, **kwargs)
#             end_time = time.time()
#             if not is_need_ret_val:
#                 # 如果不需要返回值，那么就直接打印
#                 print("函数的运行结果是：", result)
#                 print("函数运行所花费的时间是：", end_time - start_time)
#                 return None
#             return result
#
#         return inner
#
#     return cost_time


# 计算caculate_sum所耗费的时间
@cost_time  # @cost_time 等价于caculate_sum = cost_time(caculate_sum)
# @outer(False)  # 给装饰器传递参数 outer(111) == cost_time
def caculate_sum(num):
    """求1到任意数字的和"""
    total = 0
    for i in range(1, num + 1):
        total += i
    return total


# start_time = time.time()
# caculate_sum()
# end_time = time.time()
# time_consuming = end_time-start_time
# print(time_consuming)


# caculate_sum = cost_time(caculate_sum)
total = caculate_sum(5000000)
print(total)
